/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wargame.dao;

import wargame.entity.*;
import connectionmanager.DB;
import java.util.*;

/**
 *
 * @author Clarence
 */
public class PlayerDAO {

    public static Player retrievePlayer(int player_id) {
        String sql = "SELECT `player_id`, `player_name`, `company_name`, `balance`, `track_record_points` FROM `player` WHERE player_id = " + player_id;
        DB db = new DB();
        db.connect();
        db.query(sql);
        Player p = null;
        if(db.next()){
            p = populate(db);
        }
        db.close();
        return p;
    }

    private static Player populate(DB db) {
        Player p = new Player();
        p.setPlayer_id(db.getInt("player_id"));
        p.setPlayer_name(db.getString("player_name"));
        p.setCompany_name(db.getString("company_name"));
        p.setBalance(db.getInt("balance"));
        p.setTrack_record_points(db.getInt("track_record_points"));
        return p;
    }

    public static int insertPlayer(Player p) {
        String sql = "INSERT INTO player (`player_name`, `company_name`, `balance`, "
                + "`track_record_points`) VALUES ('"
                + p.getPlayer_name() + "', '" + p.getCompany_name() + "', '" + p.getBalance()
                + "', '" + p.getTrack_record_points() + "');";
        DB db = new DB();
        db.connect();
        db.execute(sql);
        int lastInsertedId = db.getLastInsertedID();
        db.close();
        return lastInsertedId;
    }

    public static void updatePlayer(Player p) {
        String sql = "UPDATE player SET `player_name` = '" + p.getPlayer_name()
                + "', `company_name` = '" + p.getCompany_name()
                + "', `balance` = '" + p.getBalance()
                + "', `track_record_points` = '" + p.getTrack_record_points()
                + "' WHERE player_id = '" + p.getPlayer_id() + "';";
        DB db = new DB();
        db.connect();
        db.execute(sql);
        db.close();
    }

    // update balance and track record points
    public static void updatePlayerRecords(Player p) {
        String sql = "UPDATE player SET `balance` = '" + p.getBalance()
                + "', `track_record_points` = '" + p.getTrack_record_points()
                + "' WHERE player_id = '" + p.getPlayer_id() + "';";
        DB db = new DB();
        db.connect();
        db.execute(sql);
        db.close();
    }

    public static void updatePlayerBalance(int player_id, int balance) {
        String sql = "UPDATE player SET balance =" + balance +" WHERE player_id=" + player_id;
        DB db = new DB();
        db.connect();
        db.execute(sql);
        db.close();
    }

    public static Player retrievePlayerByCompanyName(String company_name) {
        String sql = "SELECT * FROM `player` WHERE company_name = '" + company_name + "'";
        DB db = new DB();
        db.connect();
        db.query(sql);
        Player p = null;
        if (db.next()) {
            p = populate(db);
        }
        db.close();
        return p;
    }

    public static ArrayList<Player> retrieveAllPlayers() {
        ArrayList<Player> players = new ArrayList<Player>();
        String sql = "SELECT * FROM `player`";
        DB db = new DB();
        db.connect();
        db.query(sql);
        while (db.next()) {
            Player p = populate(db);
            players.add(p);
        }
        db.close();
        return players;
    }


    public static ArrayList<Player> retrieveAllPlayersOrderByWinner() {
        ArrayList<Player> players = new ArrayList<Player>();
        String sql = "SELECT * FROM `player` ORDER BY `balance` DESC";
        DB db = new DB();
        db.connect();
        db.query(sql);
        while (db.next()) {
            Player p = populate(db);
            players.add(p);
        }
        db.close();
        return players;
    }

    public static Player retrievePlayerByName(String player_name) {
        String sql = "SELECT * FROM `player` WHERE player_name = '" + player_name + "'";
        DB db = new DB();
        db.connect();
        db.query(sql);
        Player p = null;
        if (db.next()) {
            p = populate(db);
        }
        db.close();
        return p;
    }

    /*public static void updatePlayerEmployees(int employee_id){
        String sql = "DELETE FROM employee where employee_id='"+employee_id+"'";
        DB db = new DB();
        db.connect();
        db.execute(sql);
        db.close();
    }*/

    public static void clear() {
        String sql = "truncate player;";
        DB db = new DB();
        db.connect();
        db.execute(sql);
        db.close();
    }
}
